<template>
  <div class="register-container">
    <div class="register-box">
      <div class="register-header">
        <img src="@/assets/images/logo.svg" alt="天威线材" class="register-logo">
        <h1 class="register-title">注册账号</h1>
      </div>

      <el-form
        ref="registerFormRef"
        :model="registerForm"
        :rules="registerRules"
        class="register-form"
        @keyup.enter="handleRegister"
      >
        <el-form-item prop="username">
          <el-input
            v-model="registerForm.username"
            placeholder="用户名"
            prefix-icon="User"
            clearable
          />
        </el-form-item>

        <el-form-item prop="name">
          <el-input
            v-model="registerForm.name"
            placeholder="姓名"
            prefix-icon="UserFilled"
            clearable
          />
        </el-form-item>

        <el-form-item prop="email">
          <el-input
            v-model="registerForm.email"
            placeholder="邮箱"
            prefix-icon="Message"
            clearable
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="registerForm.password"
            type="password"
            placeholder="密码"
            prefix-icon="Lock"
            show-password
            clearable
          />
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            :loading="loading"
            class="register-button"
            @click="handleRegister"
          >
            {{ loading ? '注册中...' : '注册' }}
          </el-button>
        </el-form-item>
      </el-form>

      <div class="register-footer">
        <p>已有账号？<router-link to="/login">立即登录</router-link></p>
        <p class="copyright">© {{ new Date().getFullYear() }} 天威线材 版权所有</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, UserFilled, Message, Lock } from '@element-plus/icons-vue'
import api from '@/services/api'

const router = useRouter()
const loading = ref(false)
const registerFormRef = ref(null)

// 注册表单
const registerForm = reactive({
  username: '',
  name: '',
  email: '',
  password: ''
})

// 表单验证规则
const registerRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度应为3-20个字符', trigger: 'blur' }
  ],
  name: [
    { required: true, message: '请输入姓名', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入有效的邮箱地址', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度应为6-20个字符', trigger: 'blur' }
  ]
}

// 处理注册
const handleRegister = async () => {
  if (!registerFormRef.value) return

  try {
    await registerFormRef.value.validate()

    loading.value = true

    try {
      await api.register({
        username: registerForm.username,
        name: registerForm.name,
        email: registerForm.email,
        password: registerForm.password
      })

      ElMessage.success('注册成功，请登录')

      // 跳转到登录页面
      router.push({ path: '/login' })
    } catch (error) {
      console.error('注册失败:', error)
      ElMessage.error(error.response?.data?.message || '注册失败，请检查输入信息')
    } finally {
      loading.value = false
    }
  } catch (error) {
    console.error('表单验证失败:', error)
    ElMessage.warning('请正确填写注册信息')
  }
}
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(135deg, #041633 0%, #0a1929 100%);
  overflow: hidden;
  position: relative;
}

.register-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('@/assets/images/bg-pattern.svg') center/cover no-repeat;
  opacity: 0.05;
  z-index: 0;
}

.register-box {
  width: 400px;
  padding: 40px;
  background: rgba(0, 21, 41, 0.7);
  border-radius: 8px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
  z-index: 1;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(24, 144, 255, 0.2);
  position: relative;
}

.register-header {
  text-align: center;
  margin-bottom: 30px;
}

.register-logo {
  height: 60px;
  margin-bottom: 15px;
}

.register-title {
  font-size: 24px;
  color: #fff;
  margin: 0;
  font-weight: 500;
  letter-spacing: 1px;
  text-shadow: 0 0 10px rgba(24, 144, 255, 0.5);
}

.register-form {
  margin-bottom: 20px;
}

.register-button {
  width: 100%;
  height: 40px;
  font-size: 16px;
  background: linear-gradient(90deg, #1890ff, #36cfc9);
  border: none;
}

.register-button:hover {
  background: linear-gradient(90deg, #40a9ff, #5cdbd3);
  border: none;
}

.register-footer {
  text-align: center;
  color: rgba(255, 255, 255, 0.6);
  font-size: 14px;
}

.register-footer a {
  color: #1890ff;
  text-decoration: none;
}

.register-footer a:hover {
  text-decoration: underline;
}

.copyright {
  margin-top: 10px;
  font-size: 12px;
  color: rgba(255, 255, 255, 0.4);
}

/* 响应式调整 */
@media (max-width: 480px) {
  .register-box {
    width: 90%;
    padding: 30px 20px;
  }

  .register-logo {
    height: 50px;
  }

  .register-title {
    font-size: 20px;
  }
}
</style>
